/*
 * Copyright (c) 2010 Toni Spets <toni.spets@iki.fi>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <ctype.h>
#include <windows.h>

#include "sockets.h"
#include "config.h"

#define INI_NAME "cncnet.ini"

char        config_server[64] = "hifi.iki.fi";
uint16_t    config_server_port = NET_PORT;
uint16_t    config_port = NET_PORT;
uint8_t     config_port_force = FALSE;

void config_init()
{
    char cwd[MAX_PATH];
    char ini_path[MAX_PATH];
    GetCurrentDirectory(sizeof(cwd), cwd);
    snprintf(ini_path, sizeof(ini_path), "%s\\%s", cwd, INI_NAME);

    char tmp[2] = { 0x00, 0x00 };
    UINT itmp;
    GetPrivateProfileString("CnCNet", "Server", config_server, config_server, sizeof(config_server), ini_path);

    itmp = GetPrivateProfileInt("CnCNet", "ServerPort", config_server_port, ini_path);
    if(itmp > 1024 && itmp < 65535)
    {
        config_server_port = (uint16_t)itmp;
    }

    itmp = GetPrivateProfileInt("CnCNet", "Port", config_port, ini_path);
    if(itmp > 1024 && itmp < 65535)
    {
        config_port = (uint16_t)itmp;
    }

    GetPrivateProfileString("CnCNet", "PortForce", "n", tmp, sizeof(tmp), ini_path);
    if(tolower(tmp[0]) == 'y' || tolower(tmp[0]) == 'e'|| tolower(tmp[0]) == 't' || tmp[0] == '1')
    {
        config_port_force = TRUE;
    }
    else 
    {
        config_port_force = FALSE;
    }
}
